<?php 
/**
 * 用户模块
 */
header("Content-Type: text/html; charset=UTF-8");
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
	private $user_id;//用户id
	private $username;//用户名
	private $pwd_salt = 'dodo';//密码加密字符串 不可修改
	private $base_url;//首页
	function __construct()
	{
		parent::__construct();	
		$ary_session = array(
			'user_id'=>1,
			'username'=>'dodo',
		);
		$this->session->set_userdata($ary_session);
		$this->user_id = $this->session->userdata('user_id');
		$this->username = $this->session->userdata('username');
		$this->base_url = base_url();
		//判断登录
		if( empty($this->user_id) ){
			$login_url = site_url(array('home','login'));
			echo "<script language='javascript'>alert('请先登录');window.location.href='{$login_url}';</script>";
			exit;
		}
	}
	/**
	 * 首页
	 */
	public function index()
	{
		//phpinfo();
		$data['name'] = empty($this->username)?'游客':$this->username;
		$this->load->view('user/index',$data);
	}
	/**
	 * 修改密码
	 */
	function modify_pwd()
	{
		if (!is_post()){
			$this->load->view('user/modify_pwd');	
		}else{
			$old_pwd = trim($_POST['old_pwd']);
			$new_pwd = trim($_POST['new_pwd']);
			$confirm_pwd = trim($_POST['confirm_pwd']);
			if( empty($old_pwd) || empty($new_pwd) || empty($confirm_pwd) ){
				error_return("输入不完整");
			}
			if( $new_pwd!=$confirm_pwd ){
				error_return("新密码与确认密码不一致");
			}
			$pwd_len = strlen($new_pwd);
			if( $pwd_len<4 || $pwd_len>20 ){
				error_return("密码长度为4-20个字符！");
			}
			$query_user = $this->db->select('password')->where('id',$this->user_id)->get('user');
			$row_user = $query_user->row();
			if($row_user->password != md5(md5($old_pwd).$this->pwd_salt)){
				error_return("旧密码不正确");
			}else{
				$password = md5(md5($new_pwd).$this->pwd_salt);
				$data = array(
	               'password'=>$password,
	            );
				$this->db->where('id', $this->user_id);
				$this->db->limit(1);
				$this->db->update('user', $data);
				success_return("修改成功");
			}
		}
	}
	/**
	 * 退出登录
	 */
	function logout()
	{
		$this->session->sess_destroy();
		success_return("退出游戏成功");
	}
	/**
	 * 每日登录签到
	 */
	function sign_in()
	{
		$query = $this->db->where('user_id',$this->user_id)->get('sign_in');
		$now_day = date('Ymd',time());
		$last_day = 0;
		if( $query->num_rows()==0 ){
			$insert = array();
			$insert['user_id'] = $this->user_id;
			$insert['last_day'] = 1;
			$insert['last_login_day'] = $now_day;
			$this->db->insert('sign_in',$insert);
			$last_day = 1;
		}else{
			$row = $query->row();
			$yesterday = date('Ymd',strtotime("-1 day"));
			if( $row->last_day == $yesterday ){
				$this->db->set('last_day',"last_day+1", FALSE);
				$last_day = $row->last_day + 1;
			}
			if( $last_day!=0 ){
				$this->db->limit(1);
		    	$this->db->where('user_id', $this->user_id);
		    	$this->db->update('sign_in');
			}
		}	
		//加分
		if( $last_day>0 ){
			$max_sign_in_bonus = get_base('max_sign_in_bonus');
			$last_day = $last_day>$max_sign_in_bonus?$max_sign_in_bonus:$last_day;
			$this->common->add_coin($this->user_id,'sign_in',$last_day);
			success_return("签到成功!获得{$last_day}D币");
		}
		error_return("你已经签过到了^_^");
	}
	/**
	 * 我的建议
	 * $type all、new、reply  
	 */
	function suggest()
	{
		$type = '';
		if( isset($_GET['type']) ){
			$type = trim($_GET['type']);
		}
		$ary_type = array('new','reply');
		if( !in_array($type,$ary_type) ){
			$type = 'all';
		}
		$where['user_id'] = $this->user_id;
		if( $type=='new' ){
			$where['reply_time'] = 0;
		}elseif( $type=='reply' ){
			$where['reply_time !='] = 0;
		}
		$total_rows = $this->db->where($where)->from('suggest')->count_all_results();
		$per_page = 10;
		$this->load->library('page',array('total'=>$total_rows,'perpage'=>$per_page));
		$data['page'] = $this->page->show(1);
		$query = $this->db->where($where)->order_by('id desc')->get('suggest', $per_page, $this->page->offset());
        $data['results'] = $query->result();
        $this->load->view('user/suggest', $data);
	}
	/**
	 * 我的建议详情
	 * int $id suggest中的唯一id
	 */
	function suggest_detail($id=0)
	{
		$url = site_url('user/suggest');
		if( empty($id) or !preg_match('/^[0-9]/',$id) ){
			display('参数错误',$url,0);
		}else{
			$where = array(
				'id'=>$id,	
				'user_id'=>$this->user_id,
			);
			$query = $this->db->where($where)->get('suggest');
			if( $query->num_rows()==0 ){
				display('找不到相关记录!',$url,0);
			}else{
				$data['row'] = $query->row();
	        	$this->load->view('user/suggest_detail', $data);
			}
		}
	}
	/**
	 * 我的积分
	 * $type all、new、reply  
	 */
	function coin()
	{
		$where['user_id'] = $this->user_id;
		$total_rows = $this->db->where($where)->from('coin_log')->count_all_results();
		$per_page = 10;
		$this->load->library('page',array('total'=>$total_rows,'perpage'=>$per_page));
		$data['page'] = $this->page->show(1);
		$query = $this->db->where($where)->order_by('id desc')->get('coin_log', $per_page, $this->page->offset());
        $data['results'] = $query->result();
        $query_user = $this->db->select('max_coin,coin')->where('id',$this->user_id)->get('user');
        $data['user_info'] = $query_user->row();
        $this->load->view('user/coin', $data);
	}
	/**
	 * 完善个人信息
	 */
	function complete()
	{
		$query_user = $this->db->select('is_complete')->where('id',$this->user_id)->get('user');
		$row_user = $query_user->row();
		if (!is_post()){
			$data['is_complete'] = $row_user->is_complete;
			$this->load->view('user/complete',$data);	
		}else{
			$sex = trim($_POST['sex']);
			$birthday = trim($_POST['birthday']);
			$hobby = trim($_POST['hobby']);
			$true_name = trim($_POST['true_name']);
			if( empty($sex) || empty($birthday) || empty($hobby) || empty($true_name) ){
				error_return("输入不完整");
			}
			
			$data = array(
               'sex'=>$sex,
               'birthday'=>$birthday,
               'hobby'=>$hobby,
               'true_name'=>$true_name,
            );
            //未完善过信息
            $return_str = "修改个人信息成功";
            if( ($row_user->is_complete&1)==0 ){
            	$data['is_complete'] = $row_user->is_complete + 1;
            	$coin = get_base('complete_base_info');
            	$this->common->add_coin($this->user_id,'complete_base_info',$coin);
				$return_str = "完善个人信息成功!获得{$coin}D币";
            }
			$this->db->where('id', $this->user_id);
			$this->db->limit(1);
			$this->db->update('user', $data);
			success_return($return_str);
		}
	}
}
